package com.xianbeiying.app.ckgl.photo.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.activity.BaseActivity;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.ImageModel;
import com.xianbeiying.app.ckgl.photo.adapter.HistoryPhotoGridAdapter;
import com.xianbeiying.app.ckgl.photo.event.OnZoomListener;
import com.xianbeiying.app.ckgl.photo.fragment.HistoryPickerFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 历史图片
 * */
public class HistoryPictureActivity extends BaseActivity implements HttpCallBackUtil
{
    RecyclerView recyclerView;
    private HistoryPhotoGridAdapter historyAdapter;
    private ImageModel imageModel;
    private HistoryPickerFragment imagePagerFragment;
    private HistoryPickerFragment selectedPagerFragment;
    private ImageView iv_title_left;
    private TextView tv_title;
    private  SwipeRefreshLayout mSwipeRefreshLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_picture);
        initView();
    }
    private void initView(){
        imageModel=new ImageModel(this);
        imageModel.addResponseListener(this);
        mSwipeRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.swiperefreshlayout);
        tv_title=(TextView) findViewById(R.id.tv_title);
        tv_title.setText("历史图片");
        iv_title_left=(ImageView) findViewById(R.id.iv_title_left);
         recyclerView = (RecyclerView)findViewById(R.id.rv_photos);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(4, OrientationHelper.VERTICAL);
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
        recyclerView.setLayoutManager(layoutManager);
        imageModel.ImageHistoLogins(com.xianbeiying.app.ckgl.util.DataUtil.getLoactionTime(),getIntent().getIntExtra("id",0),true);
        iv_title_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                finish();
            }
        });

        initEvent();
    }

    private void initEvent() {

        mSwipeRefreshLayout.setColorSchemeColors(Color.RED, Color.BLUE);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(tiemUrl.size()!=0)
                imageModel.ImageHistoLogins(tiemUrl.get(tiemUrl.size()-1),getIntent().getIntExtra("id",0),true);
            }
        });
    }


    public void addImagePagerFragment(HistoryPickerFragment imagePagerFragment, boolean isSelected) {
        if (isSelected) {
            this.selectedPagerFragment = imagePagerFragment;
            if (this.imagePagerFragment != null && this.imagePagerFragment.isVisible()) {
                if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                    getSupportFragmentManager().popBackStack();
                }
            }
        } else {
            this.imagePagerFragment = imagePagerFragment;
            if (this.selectedPagerFragment != null && this.selectedPagerFragment.isVisible()) {
                if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                    getSupportFragmentManager().popBackStack();
                }
            }

        }
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.container, imagePagerFragment)
                .addToBackStack(null)
                .commit();
    }
      List<String> imgUrl=new ArrayList<>();
      List<String> tiemUrl=new ArrayList<>();
    @Override
    public void onCallBackData(String url, Object content) {
        JSONArray jsonArray= JSON.parseArray(content+"");
        if(jsonArray!=null&&jsonArray.size()!=0){
            int size=jsonArray.size();
            for(int i=0;i<size;i++){
                imgUrl.add(jsonArray.getString(i));
                String [] dateStr=jsonArray.getString(i).split("\\/");
                String [] date=dateStr[dateStr.length-1].split("\\.");
                tiemUrl.add(date[0]);

            }
//            String dates=imgUrl.get(size-1);
//            String [] dateStr=dates.split("\\/");
//            String [] date=dateStr[dateStr.length-1].split("\\.");
//            LogUtils.e("-----",date[0]);
            if (mSwipeRefreshLayout != null) {
                mSwipeRefreshLayout.setRefreshing(false);
            }
            historyAdapter = new HistoryPhotoGridAdapter(this, imgUrl);
            recyclerView.setAdapter(historyAdapter);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            historyAdapter.setOnZoomListener(new OnZoomListener() {
                @Override
                public void onClick(View v, int position) {
                    int[] screenLocation = new int[2];
                    v.getLocationOnScreen(screenLocation);
                    HistoryPickerFragment imagePagerFragment =
                            HistoryPickerFragment.newInstance(imgUrl,tiemUrl, position, screenLocation,
                                    v.getWidth(), v.getHeight());
                       addImagePagerFragment(imagePagerFragment,false);
                }
            });
        }else{
            if (mSwipeRefreshLayout != null) {
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }
    }

}
